<template>
  <div id="userLayout" class="user-layout-wrapper">
    <div class="container">
      <div class="top">
        <div class="header">
          <img src="~@/assets/image/logo.jpg" class="logo" alt="logo">
          <span class="title">博客系统登陆</span>
        </div>
        <div class="desc">博客系统登陆</div>
      </div>
      <div class="main">
        <a-form
          id="formLogin"
          class="user-layout-login"
          ref="formLogin"
          :form="form"
          @submit="handleSubmit"
        >
          <a-tabs
            :activeKey="customActiveKey" @change="tabChange"
            :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
          >
            <a-tab-pane key="tab1" tab="账号密码登录">
              <a-form-item>
                <a-input
                  size="large" name="username"
                  type="text"
                  placeholder="请输入用户名"
                  v-decorator="[
                'username',
                {rules: [{ required: true, message: '请输入用户名' }, { validator: handleUsernameOrEmail }], validateTrigger: 'change'}
              ]"
                >
                  <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
                </a-input>
              </a-form-item>

              <a-form-item>
                <a-input
                  size="large" name="password"
                  type="password"
                  autocomplete="false"
                  placeholder="请输入密码"
                  v-decorator="[
                'password',
                {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
              ]"
                >
                  <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
                </a-input>
              </a-form-item>
            </a-tab-pane>
            <a-tab-pane key="tab2" tab="手机号登录">
              <a-form-item>
                <a-input size="large" placeholder="请输入手机号">
                  <a-icon slot="prefix" type="mobile"/>
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-row :gutter="8" style="margin: 0 -4px">
                  <a-col :span="16">
                    <a-input size="large" placeholder="请输入验证码">
                      <a-icon slot="prefix" type="mail"/>
                    </a-input>
                  </a-col>
                  <a-col :span="8" style="padding-left: 4px">
                    <a-button style="width: 100%" @click="sendLoginCode" class="captcha-button" size="large">获取验证码
                    </a-button>
                  </a-col>
                </a-row>
              </a-form-item>
            </a-tab-pane>
          </a-tabs>
          <a-form-item>
            <a href="javascript:;" @click="forgetPassword" class="forge-password"
               style="float: right;">
              忘记密码?
            </a>
          </a-form-item>

          <a-form-item style="margin-top:24px">
            <a-button
              size="large"
              type="primary"
              htmlType="submit"
              class="login-button"
              :loading="state.loginBtn"
              :disabled="state.loginBtn"
            >确定
            </a-button>
          </a-form-item>
        </a-form>
      </div>
      <div class="footer">
        <div class="copyright">
          Copyright &copy; {{ this.year }} 猫哨优品软件研发组出品
        </div>
      </div>
    </div>
  </div>
</template>
<script>

import {getUserInfo, login} from "@/network/login";

export default {
  name: 'app',
  data() {
    return {
      year: new Date().getFullYear(),
      customActiveKey: 'tab1',
      loginBtn: false,
      form: this.$form.createForm(this),
      // service: null,// 登陆成功后重定向地址
      state: {
        time: 60,
        loginBtn: false,
        smsSendBtn: false
      }
    }
  },
  mounted() {
    removeToken();
  },
  methods: {
    tabChange(key) {
      this.customActiveKey = key;
    },
    sendLoginCode() {
      this.$message.error("请先使用账号密码登陆");
    },
    handleUsernameOrEmail(rule, value, callback) {
      if (value == null || '' === value) {
        callback(rule.message);
      }
      callback()
    },
    forgetPassword() {
      this.$message.error('请与管理员联系');
    },
    handleSubmit(e) {
      e.preventDefault();
      const {
        form: {validateFields},
        state
      } = this;
      state.loginBtn = true;
      if (this.customActiveKey === 'tab1') {
        const validateFieldsKey = ['username', 'password'];
        validateFields(validateFieldsKey, {force: true}, (err, values) => {
          if (!err) {
            login(values.username, values.password).then(response => {
              saveToken(response.data);
              this.$store.commit(updateExpireSession, false);
              this.state.loginBtn = false;
              this.$router.replace("/")
            }).catch(err => {
              this.$message.error(err.message || '登陆失败');
              state.loginBtn = false;
            });
          } else {
            setTimeout(() => {
              state.loginBtn = false
            }, 300)
          }
        })
      } else {
        this.$message.error("请先使用账号密码登陆");
        state.loginBtn = false;
      }
    }
  }
}
import {setUserInfo, updateExpireSession} from "@/store/mutations-types";

import {exists, save} from "@/utils/token-utils";
import {removeToken, saveToken} from "@/utils/token-util";
import store from "@/store";
</script>

<style lang="less" scoped>
#userLayout.user-layout-wrapper {
  height: 100%;

  &.mobile {
    .container {
      .main {
        max-width: 368px;
        width: 98%;
      }
    }
  }

  .container {
    width: 100%;
    min-height: 100%;
    background: #f0f2f5 url(~@/assets/background.svg) no-repeat 50%;
    background-size: 100%;
    padding: 110px 0 144px;
    position: relative;

    a {
      text-decoration: none;
    }

    .top {
      text-align: center;

      .header {
        height: 44px;
        line-height: 44px;

        .badge {
          position: absolute;
          display: inline-block;
          line-height: 1;
          vertical-align: middle;
          margin-left: -12px;
          margin-top: -10px;
          opacity: 0.8;
        }

        .logo {
          height: 44px;
          vertical-align: top;
          margin-right: 16px;
          border-style: none;
        }

        .title {
          font-size: 33px;
          color: rgba(0, 0, 0, .85);
          font-family: Avenir, 'Helvetica Neue', Arial, Helvetica, sans-serif;
          font-weight: 600;
          position: relative;
          top: 2px;
        }
      }

      .desc {
        font-size: 14px;
        color: rgba(0, 0, 0, 0.45);
        margin-top: 12px;
        margin-bottom: 40px;
      }
    }

    .main {
      min-width: 260px;
      width: 368px;
      margin: 0 auto;
    }

    .footer {
      position: absolute;
      width: 100%;
      bottom: 0;
      padding: 0 16px;
      margin: 48px 0 24px;
      text-align: center;

      .links {
        margin-bottom: 8px;
        font-size: 14px;

        a {
          color: rgba(0, 0, 0, 0.45);
          transition: all 0.3s;

          &:not(:last-child) {
            margin-right: 40px;
          }
        }
      }

      .copyright {
        color: rgba(0, 0, 0, 0.45);
        font-size: 14px;
      }
    }
  }
}

.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
